/*
 * INT.c
 *
 *  Created on: 2012-9-21
 *      Author: Administrator
 */
#define INT_MODE 	52

#include "tangoc.h"


void INT_menu()
{
	puts("\nINT mode menu, press a key in brackes.");
	puts("[R] Read INT");
	puts("[W] Write INT");
	puts("[B] Back main menu");
	puts("[X] Exit");
}

void read_int_mode()
{
	int i;
	unsigned char wbuf[1], rbuf[1];

	ioctl(gdev, DEFAULT, 0);
	wbuf[0] = INT_MODE;
	for(i=0; i<3; i++)
	{
		write(gdev, wbuf, 1);
		read(gdev, rbuf, 1);
		if (rbuf[0]) break;
		msleep(100);
	}
	printf("INT mode(hex):0x0%x\n", rbuf[0]);
}

void write_int_mode()
{
	unsigned char dat, wbuf[2];

	puts("INT mode:(1)0x08 (2)0x09 (3)0x0a");
	puts("Press a key in brackes.");
	printf("Input num: ");
	scanf("%d", &dat);
	while (getchar() != '\n');
	switch (dat)
	{
	case 1:
		dat = 0x08;
		break;
	case 2:
		dat = 0x09;
		break;
	case 3:
		dat = 0x0a;
		break;
	default:
		dat = 0x0a;
		break;
	}

	ioctl(gdev, DEFAULT, 0);
	wbuf[0] = INT_MODE;
	wbuf[1] = dat & 0xFF;
	write(gdev, wbuf, 2);
	msleep(300);
}

void INT_mode()
{
	char cmd, flag = 'n';
	int type = 1;
	unsigned char loop = 1;

	INT_menu();
	while (loop)
	{
		printf("Input cmd: ");
		cmd = getchar();
		while (getchar() != '\n');
		switch (cmd)
		{
		case 'w':
		case 'W':
			write_int_mode();
			break;
		case 'r':
		case 'R':
			read_int_mode();
			break;
		case 'b':
		case 'B':
			loop = 0;
			main_menu();
			break;
		case 'x':
		case 'X':
			finish();
			break;
		}
		INT_menu();
	}
}
